package com.softa.web.controller.system;

import javax.annotation.Resource;

import com.github.pagehelper.PageInfo;
import com.softa.service.system.ResourceService;
import com.softa.vo.DataGridResponse;
import com.softa.vo.QueryRequest;
import com.softa.web.controller.base.BaseController;
import com.softa.utils.Constants;
import com.softa.vo.DataGridRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.softa.dao.model.SysRole;
import com.softa.service.system.RoleService;

/**
 * 
* @ClassName: RoleController
* @Description: TODO
* @author yangl
*
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    @Resource
    private ResourceService resourceService;

    @RequiresPermissions("role:view")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
//        model.addAttribute("roleList", roleService.findAll());
        model.addAttribute("resourceList", resourceService.findAll());
        return Constants.MENU_MANAGE+"/role/list";
    }
    
    @RequiresPermissions("role:view")
    @RequestMapping(value="/listPage",method = RequestMethod.POST)
    @ResponseBody
    public DataGridResponse listPage(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<SysRole> pageResult = roleService.queryRoleByParam(request, queryRequest);
        return new DataGridResponse(pageResult);
    }

    @RequiresPermissions("role:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        setCommonData(model);
        model.addAttribute("role", new SysRole());
        model.addAttribute("op", "新增");
        return Constants.MENU_MANAGE+"/role/edit";
    }

    @RequiresPermissions("role:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String create(SysRole role) {
        String message="录入成功";
    	try {
    		message=roleService.createRole(role);
		} catch (Exception e) {
			message="录入失败";
			logger.error(e.getMessage());
		}
        return message;
    }

    @RequiresPermissions("role:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        setCommonData(model);
        model.addAttribute("role", roleService.findOne(id));
        model.addAttribute("op", "修改");
        return Constants.MENU_MANAGE+"/role/edit";
    }

    @RequiresPermissions("role:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(SysRole role) {
        String message="修改成功";
    	try {
    		message=roleService.updateRole(role);
		} catch (Exception e) {
			message="修改失败";
			logger.error(e.getMessage());
		}
        return message;
    }

    @RequiresPermissions("role:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") Long id, Model model) {
        setCommonData(model);
        model.addAttribute("role", roleService.findOne(id));
        model.addAttribute("op", "删除");
        return Constants.MENU_MANAGE+"/role/edit";
    }

    @RequiresPermissions("role:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@PathVariable("id") Long id) {
        String message="删除成功";
    	try {
    		roleService.deleteRole(id);
		} catch (Exception e) {
			message="删除失败";
			logger.error(e.getMessage());
		}
        return message;
    }
    
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/addpage", method = RequestMethod.GET)
    public String add() {
        return Constants.MENU_MANAGE+"/role/add";
    }
    
    @RequiresPermissions("role:view")
    @RequestMapping(value = "/right", method = RequestMethod.GET)
    public String right() {
        return Constants.MENU_MANAGE+"/role/right";
    }

    private void setCommonData(Model model) {
        model.addAttribute("resourceList", resourceService.findAll());
    }

}
